package com.lizonghui.googleplay.fragment;

import android.os.SystemClock;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.lizonghui.googleplay.base.BaseFragment;
import com.lizonghui.googleplay.base.BaseHolder;
import com.lizonghui.googleplay.base.LoadingPager;
import com.lizonghui.googleplay.base.SuperBaseAdapter;
import com.lizonghui.googleplay.bean.CategoryInfoBean;
import com.lizonghui.googleplay.control.CategoryTitleHolder;
import com.lizonghui.googleplay.control.CategoryNormalHolder;
import com.lizonghui.googleplay.factory.ListViewFactory;
import com.lizonghui.googleplay.protocol.CategoryProtocol;
import com.lizonghui.googleplay.utils.LogUtils;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/6/16.
 */
public class CategoryFragment extends BaseFragment {

    private List<CategoryInfoBean> mCategoryInfoBeans;

    /**
     * @return
     * @des 真正的在子线程中加载数据, 得到数据
     * @called 触发加载数据, 外界希望通过loadingPager加载数据的时候, 调用这个方法
     */
    @Override
    public LoadingPager.LoadedResultState initData() {
        SystemClock.sleep(2000);// 模拟网络耗时2s种
        //获取协议请求对象
        CategoryProtocol categoryProtocol = new CategoryProtocol();
        try {
            //用协议请求对象去请求数据
            mCategoryInfoBeans = categoryProtocol.loadData(0);
            //由于视图有错误，我们打印一下返回的数据，之前要复写CategoryInfoBeans的toString方法
            LogUtils.printList(mCategoryInfoBeans);
            //校验返回
            return checkResData(categoryProtocol);
        } catch (IOException e) {
            e.printStackTrace();
            return LoadingPager.LoadedResultState.ERROR;
        }
    }

    /**
     * @return
     * @des 初始化具体的成功视图
     * @called 触发加载数据, 数据加载完成后, 并且数据加载成功后
     */
    @Override
    public View initSuccessView() {
        ListView listView = ListViewFactory.createListView();
        listView.setAdapter(new CategoryAdapter(listView,mCategoryInfoBeans));
        return listView;
    }

   class CategoryAdapter extends SuperBaseAdapter<CategoryInfoBean>{

       public CategoryAdapter(AbsListView absListView, List datas) {
           super(absListView, datas);
       }


       //获取Holder，就是data+view去绑定
       //由视图和数据一同决定holder
       @Override
       public BaseHolder getSpecialHolder(int position) {
           CategoryInfoBean bean = mCategoryInfoBeans.get(position);
           if(bean.isTitle){
               return new CategoryTitleHolder();//如果数据是title就返回这个holder
           }else {
               return new CategoryNormalHolder();//否则返回正常normal的holder
           }
       }

       @Override
       public int getViewTypeCount() {
           return 3;//因为多了一个类型，1个加载更多2正常-title3正常-normal
       }


       //根据是否是title分别返回不同ViewType，这样复用convertView就不会错乱
       @Override
       public int getNormalItemViewType(int position) {
           CategoryInfoBean bean = mCategoryInfoBeans.get(position);
           if(bean.isTitle){
               return 2;
           }else {
               return 1;
           }
       }
   }
}